package com.lz.sports.bean

import android.os.Parcel
import android.os.Parcelable
import android.text.TextUtils
import androidx.annotation.NonNull
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableInt
import androidx.room.*
import com.lz.library.proguard.IProguardField

@Entity(tableName = "DeviceInfo", indices = [Index("mac")])
class DeviceInfo() : Parcelable, IProguardField {

    companion object {

        //初始化的组id, 即未分组
        const val UNKNOWN_ID = -1

        //辅助类型的组
        const val ASSIST_ID = 0

        //低电量
        const val BATTERY_LOW = 20

        //初始化的未知电量
        const val INIT_BATTERY = -1

        @JvmField
        val CREATOR = object : Parcelable.Creator<DeviceInfo> {
            override fun createFromParcel(parcel: Parcel): DeviceInfo {
                return DeviceInfo(parcel)
            }

            override fun newArray(size: Int): Array<DeviceInfo?> {
                return arrayOfNulls(size)
            }
        }

        //是否为辅助设备
        fun isAssist(groupId: Int): Boolean = groupId == ASSIST_ID

        //是否已经分组
        fun hasGroup(groupId: Int): Boolean = groupId > ASSIST_ID
    }

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    var id: Int = 0

    @ColumnInfo(name = "projectId")
    var projectId: Int = 0

    //设备组ID
    @ColumnInfo(name = "groupId")
    var groupId: Int = UNKNOWN_ID

    //mac地址
    @ColumnInfo(name = "mac")
    @NonNull
    var mac: String = ""

    @ColumnInfo(name = "name")
    @NonNull
    var name: String = ""

    //别名
    @ColumnInfo(name = "alias")
    @NonNull
    var alias: String = ""

    //密码, 暂时无用
    @ColumnInfo(name = "password")
    var password: String = ""

    //添加的序号
    @ColumnInfo(name = "number")
    @NonNull
    var number: Int = 0

    //功能权限, 辅助设备时要用到, 对应   0000  后四位为考生签到, 检录, 数据查询, 成绩查询
    @ColumnInfo(name = "permission")
    var permission: Int = 0

    //是否已经扫描绑定过
    @Ignore
    var hasBinding = false

    //信号
    @Ignore
    var rssi: String = ""

    //电量
    @Ignore
    var battery = ObservableInt(INIT_BATTERY)

    //是否选中
    @Ignore
    var isSelected = ObservableBoolean(false)

    //是否连上线
    @Ignore
    var isOnLine = ObservableBoolean(false)

    fun bindGroup(groupId: Int, number: Int) {
        this.groupId = groupId
        this.number = number
    }

    fun unbindGroup() {
        this.groupId = UNKNOWN_ID
        this.number = 0
    }

    fun changeLine(isOnLine: Boolean) {
        if (this.isOnLine.get() == isOnLine) return
        this.isOnLine.set(isOnLine)
    }

    //设置电量
    fun modifyBattery(battery: Int) {
        this.battery.set(battery)
        changeLine(true)
    }

    fun resetBattery() {
        isOnLine.set(false)
        battery.set(INIT_BATTERY)
    }

    //是否为辅助机
    fun isAssist(): Boolean = Companion.isAssist(groupId)

    //是否已经分组
    fun hasGroup(): Boolean = Companion.hasGroup(groupId)

    //更改是否为辅助设备
    fun changeAssist(isAssist: Boolean): Boolean {
        if (isAssist() == isAssist) return false
        groupId = if (isAssist) ASSIST_ID else UNKNOWN_ID
        return true
    }

    //显示的名称
    fun showName(): String = if (TextUtils.isEmpty(alias)) name else alias

    //权限
    fun indexPermission(index: Int): Boolean = permission.shr(index).and(1) == 1

    fun select() {
        if (!hasBinding) isSelected.set(!isSelected.get())
    }

    fun needSelect() {
        if (!hasBinding && !isSelected.get()) select()
    }

    fun hasSelected(): Boolean = !hasBinding && isSelected.get()

    override fun equals(other: Any?): Boolean {
        if (other === this) return true
        if (other is DeviceInfo) return other.projectId == this.projectId && other.mac == this.mac
        return false
    }

    override fun hashCode(): Int {
        var result = 17
        result = 31 * result + projectId.hashCode()
        result = 31 * result + mac.hashCode()
        return result
    }

    constructor(parcel: Parcel) : this() {
        id = parcel.readInt()
        projectId = parcel.readInt()
        groupId = parcel.readInt()
        mac = parcel.readString()!!
        name = parcel.readString()!!
        alias = parcel.readString()!!
        password = parcel.readString()!!
        number = parcel.readInt()
        permission = parcel.readInt()
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(id)
        parcel.writeInt(projectId)
        parcel.writeInt(groupId)
        parcel.writeString(mac)
        parcel.writeString(name)
        parcel.writeString(alias)
        parcel.writeString(password)
        parcel.writeInt(number)
        parcel.writeInt(permission)
    }

    override fun describeContents(): Int = 0

    override fun toString(): String = "DeviceInfo: id = $id, projectId = $projectId, groupId = $groupId, name = $name, " +
            "mac = $mac, alias = $alias, password = $password, number = $number, permission = $permission"
}